package com.meevle.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.meevle.business.UserDelegate;
/**
 * Servlet implementation class Index
 */
public class Studio extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Map<Object,Object> pages = ApplicationServletSettings.getInstance().getStudioPages();
    private String firstPage = ApplicationServletSettings.getInstance().getFirstStudioPage();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Studio() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
    	response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try
        {
        	String logUser = request.getParameter("logUserName");
        	String logPass = request.getParameter("logPassword");
        	
        	if(logUser!=null && logPass != null && UserDelegate.login(logUser, logPass))
        	{
        		HttpSession sess = request.getSession();
        		sess.setAttribute("userName", logUser);
        	}
        	
//        	System.out.println("\n" + CommonServletSettings.getInstance().getHeader());
//        	System.out.println("\n" + CommonServletSettings.getInstance().getFooter());
        	
//        	System.out.println("\n" + (String)CommonServletSettings.getInstance().getScripts().get(0));
        	
        	request.setAttribute("scripts", ApplicationServletSettings.getInstance().getScripts());
        	request.setAttribute("cssFiles", ApplicationServletSettings.getInstance().getCssFiles());
        	request.setAttribute("firstPage", firstPage);
        	request.setAttribute("firstPage", firstPage);
        	request.setAttribute("navigation", ApplicationServletSettings.getInstance().getNavigationFactory().getNavigation(this));
        	request.setAttribute("sidebar", ApplicationServletSettings.getInstance().getSidebarFactory().getSidebar(this, this.pages, 
					(request.getParameter("page")==null?firstPage:request.getParameter("page"))
					));
        	RequestDispatcher view = request.getRequestDispatcher("com/meevle/web/pages/Studio.jsp");
			view.forward(request, response);
        }
        finally
        {
        	out.close();
        }
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
}
